/**
 *Copyright (c) 2024 watereyes
 * safetymanagement is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.scene.controller;


import com.koron.common.StaffAttribute;
import com.koron.common.bean.StaffBean;
import com.koron.common.domain.DataBean;
import com.koron.common.permission.SPIAccountAnno;
import com.koron.scene.domain.JSACorporation;
import com.koron.scene.service.api.JSACorporationService;
import com.koron.scene.utils.BusinessCode;
import com.koron.system.annotation.Log;
import com.koron.system.discrete.BusinessManipulation;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.swan.bean.MessageBean;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Arrays;
import java.util.List;

@Api(tags = "jsacorporation", description = "水司jsa接口")
@RestController
@RequestMapping("/jsacorporation")
public class JSACorporationController {

    private final static  Logger logger = LoggerFactory.getLogger(JSACorporationController.class);

    @Autowired
    private JSACorporationService jsaService;

    @ApiOperation("存储水司JSA")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "instance", value = "水司JSA对象的json格式，包含JSA的作业步骤列表", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "managerCode", value = "审核人code" , paramType = "query", dataType = "String")
    })
    @RequestMapping(value = "/save.htm", method = {RequestMethod.POST})
    @Log(title = "新增、修改水司JSA", businessManipulation = BusinessManipulation.INSERT)
    public MessageBean save(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user,
                            @RequestBody JSACorporation instance, @ApiIgnore String managerCode) {
        if (user == null) {
            MessageBean<?> msg = new MessageBean<>();
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        return jsaService.persist(user, instance, managerCode);
    }


    @ApiOperation("批量删除水司JSA")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "水司JSA Id,多个之间用逗号分开", required = true, dataType = "String", paramType = "query")
    })
    @RequestMapping(value = "/batchDelete.htm", method = RequestMethod.POST)
    @Log(title = "批量删除水司JSA", businessManipulation = BusinessManipulation.DELETE)
    public MessageBean batchDelete(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, String ids) {
        if (user == null) {
            MessageBean<?> msg = new MessageBean<>();
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        if (StringUtils.isEmpty(ids)) {
            return MessageBean.create(BusinessCode.SUCCESS.getCode(), BusinessCode.SUCCESS.getDescription(), void.class);
        }
        try {
            String[] idArray = ids.split("[,，]");
            return jsaService.batchDetele(Arrays.asList(idArray));
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
        return MessageBean.create(BusinessCode.FAILURE.getCode(), BusinessCode.FAILURE.getDescription(), void.class);
    }


    @ApiOperation("查询水司jsa列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "corpCode", value = "水司code" , dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "名称" , dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "jsaTypeId", value = "JSA类型" , dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "当前页", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", required = true, dataType = "String", paramType = "query")
    })
    @RequestMapping(value = "/list.htm", method = RequestMethod.POST)
    public MessageBean<DataBean<JSACorporation>> list(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, @RequestParam(required = false) String corpCode, @RequestParam(required = false) String name, @RequestParam(required = false) String jsaTypeId,
                                                      @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "15") Integer pageSize) {
        MessageBean<DataBean<JSACorporation>> msg = new MessageBean<>();
        if (user == null) {
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        Integer start = (page <= 1) ? 0 : (page - 1) * pageSize;
        try {
            DataBean<JSACorporation> dataBean = jsaService.fetchInstances(corpCode, name, jsaTypeId, start, pageSize);
            msg.setData(dataBean);
            msg.setCode(BusinessCode.SUCCESS.getCode());
            msg.setDescription(BusinessCode.SUCCESS.getDescription());
        } catch (Exception e) {
            msg.setCode(BusinessCode.EXCEPTION.getCode());
            msg.setDescription(BusinessCode.EXCEPTION.getDescription());
        }
        return msg;
    }

    @ApiOperation("查询水司jsa列表(不包含草稿)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "corpCode", value = "水司code" , dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "名称" , dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "jsaTypeId", value = "JSA类型" , dataType = "String", paramType = "query")
    })
    @RequestMapping(value = "/queryForSelect.htm", method = RequestMethod.POST)
    public MessageBean queryForSelect(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, @RequestParam(required = false) String corpCode, @RequestParam(required = false) String name,
                                      @RequestParam(required = false) String jsaTypeId) {
        MessageBean msg = new MessageBean<>();
        if (user == null) {
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        try {
            List<JSACorporation> dataBean = jsaService.queryJSAByCondition(corpCode, name, jsaTypeId);
            msg.setData(dataBean);
            msg.setCode(BusinessCode.SUCCESS.getCode());
            msg.setDescription(BusinessCode.SUCCESS.getDescription());
        } catch (Exception e) {
            msg.setCode(BusinessCode.EXCEPTION.getCode());
            msg.setDescription(BusinessCode.EXCEPTION.getDescription());
        }
        return msg;
    }


    @ApiOperation("查询所有模板")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "corpCode", value = "水司code" , dataType = "String", paramType = "query")
    })
    @RequestMapping(value = "/queryAll.htm", method = RequestMethod.POST)
    public MessageBean<List<JSACorporation>> queryAll(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, @RequestParam(required = false) String corpCode) {
        MessageBean<List<JSACorporation>> msg = new MessageBean<>();
        if (user == null) {
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        Integer start = null, rows = null;
        String name = null, jsaTypeId = null;
        try {
            DataBean<JSACorporation> dataBean = jsaService.fetchInstances(corpCode, name, jsaTypeId, start, rows);
            msg.setData(dataBean.getList());
            msg.setCode(BusinessCode.SUCCESS.getCode());
            msg.setDescription(BusinessCode.SUCCESS.getDescription());
        } catch (Exception e) {
            msg.setCode(BusinessCode.EXCEPTION.getCode());
            msg.setDescription(BusinessCode.EXCEPTION.getDescription());
        }
        return msg;
    }


    @ApiOperation("水司jsa详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "水司JSA Id", required = true, dataType = "String", paramType = "query")
    })
    @RequestMapping(value = "/detail.htm", method = RequestMethod.POST)
    public MessageBean<JSACorporation> detail(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, @RequestParam(required = true) final String id) {
        MessageBean<JSACorporation> msg = new MessageBean<>();
        if (user == null) {
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        if (StringUtils.isEmpty(id)) {
            msg.setCode(BusinessCode.INSUFFICIENT_PARAM.getCode());
            msg.setDescription(BusinessCode.INSUFFICIENT_PARAM.getDescription());
            return msg;
        }
        try {
            JSACorporation instance = jsaService.fetchOne(id);
            msg.setCode(BusinessCode.SUCCESS.getCode());
            msg.setDescription(BusinessCode.SUCCESS.getDescription());
            msg.setData(instance);
        } catch (Exception e) {
            msg.setCode(BusinessCode.EXCEPTION.getCode());
            msg.setDescription(BusinessCode.EXCEPTION.getDescription());
        }
        return msg;
    }
}
